import {
  getList,
  getShareSetting
} from '../../utils/service.js'

var config = require('../../config/config');
var enum_type = require('../../utils/enum');


Page({
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: false,
    autoplay: false,
    name: '',
    lists: [],
    pages: 1,
    pageSize: 10,
    currentPage: 1,
    wxSearchData: '',
    qrCode: '',
    bghide: 'none',
  },
  getShareInfo: function () {
    var that = this;
    var appId = config.appId;
    getShareSetting({
      appid: appId,
      "type": enum_type.pageEnum.SEARCH
    }).then(res => {
      that.setData({
        shareInfo: res
      })

    }).catch(e => {

    })
  },
  // 搜索框输入时候操作
  wxSearchInput: function(e) {
    var that = this;
    var inputValue = e.detail.value;
    if (inputValue == '') {
      that.setData({
        wxSearchData: ""
      });
      this.getList();
    } else {
      // 更新视图
      that.setData({
        wxSearchData: inputValue
      });
    }

  },
  previewImage: function(e) {
    debugger
    var dataset = e.currentTarget.dataset;
    var qrCode = dataset.code;
    var urls = [qrCode];
    wx.previewImage({
      current: qrCode, // 当前显示图片的http链接   
      urls: urls // 需要预览的图片http链接列表   
    })
  },

  qrCode: function(e) {
    debugger
    var dataset = e.target.dataset;
    var qrCode = dataset.qrcode;
    this.setData({
      qrCode: qrCode,
      bghide: 'block'
    })
  },
  hideExplain: function(e) {
    this.setData({
      qrCode: '',
      bghide: 'none'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var name = options.name;
    if (name != 'undefined' && name != '' && name != null) {
      this.setData({
        wxSearchData: options.name,
      })
    }

    this.getList();
    this.getShareInfo();

  },
  clear: function() {
    if (this.data.wxSearchData == null || this.data.wxSearchData == '') {
      return;
    }
    this.setData({
      wxSearchData: ''
    })
    this.getList();
  },
  getList: function() {
    var that = this;
    debugger
    getList({
      current: 1,
      size: that.data.pageSize,
      name: that.data.wxSearchData
    }).then(res => {
      that.setData({
        pages: res.pages,
        currentPage: res.current + 1
      });
      if (res.records && res.records.length > 0) {
        that.setData({
          lists: res.records,
        });
      } else {
        that.setData({
          lists: [],
        });
      }
    }).catch(e => {

    })
  },
  getListNext: function() {
    var that = this;
    var currentPage = that.data.currentPage
    if (currentPage > that.data.pages) {
      return
    }
    debugger
    getList({
      current: that.data.currentPage,
      size: that.data.pageSize,
      name: that.data.wxSearchData
    }).then(res => {
      that.setData({
        pages: res.pages,
        currentPage: res.current + 1
      });

      if (res.records && res.records.length > 0) {
        var list = that.data.lists;
        that.setData({
          lists: list.concat(res.records)
        });
      }
    }).catch(e => {

    })
  },
  onShareAppMessage: function (options) {
    
    var shareInfo = this.data.shareInfo;
    if (shareInfo) {
      return {
        title: shareInfo.title,
        imageUrl: shareInfo.imageSrc
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getListNext()
  },



})